<template>
  <CoolDialog v-model="showDialog" width="30vw" @close="close">
    <div style="height: 100%; display: flex; flex-direction: column">
      <div class="content">
        <el-form :model="nodeConfigFrom" label-width="70px">
          <el-form-item
            label="触发方式"
            v-if="lineAttr?.parentType == nodeTypeEnum.ruleNode"
          >
            <el-radio-group v-model="nodeConfigFrom.triggerType">
              <div style="display: flex; flex-direction: column">
                <el-radio :label="1">真值触发</el-radio>
                <el-radio :label="0">假值触发</el-radio>
                <el-radio :label="9">始终触发</el-radio>
              </div>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin-top: 30px; text-align: center">
        <el-button @click="onSubmit">确定</el-button>
        <el-button @click="close">{{ langMsg?.cancel || "取消" }}</el-button>
      </div>
    </div>
  </CoolDialog>
</template>
<script setup lang="ts">
import { computed, inject, onMounted, onUnmounted, ref } from "vue";
import { nodeTypeEnum } from "../const";
import CoolDialog from "@/components/CoolDialog.vue";
// import { getChainNodeDetail } from '@/api/RuleEngine'
const props = defineProps({
  lineAttr: {
    type: Object,
    default: {},
  },
  modelValue: { type: String, default: "0" },
});
const emits = defineEmits(["setConnectionLabel", "update:modelValue", "cancelLine"]);
interface node {
  id?: string;
  lineLabel?: string;
  triggerType?: number | string;
  parentType?: string | any;
}
const apis: any = inject("apis");
const langMsg: any = inject("langMsg");
const showDialog = ref(false);
const nodeConfigFrom = ref<node>({});
const close = () => {
  reset();
  emits("cancelLine", props.lineAttr.id);
};
const reset = () => {
  showDialog.value = false;
  nodeConfigFrom.value = { triggerType: 9 };
  emits("update:modelValue", 0);
};
const formLabel = computed(() => {
  let labelText = "";
  if (nodeConfigFrom.value.triggerType == 1) {
    labelText = "真值触发";
  }
  if (nodeConfigFrom.value.triggerType == 0) {
    labelText = "假值触发";
  }
  if (nodeConfigFrom.value.triggerType == 9) {
    labelText = "始终触发";
  }
  return labelText;
});
const onSubmit = () => {
  emits("setConnectionLabel", {
    lineLabel: formLabel.value,
    triggerType: nodeConfigFrom.value.triggerType,
  });
  reset();
};

onMounted(async () => {
  if (props.lineAttr.id) {
    let res = await apis?.getChainNodeDetail(props.lineAttr.id);
    let data = res.data.data;
    nodeConfigFrom.value = {
      lineLabel: data.lineLabel,
      triggerType: data.triggerType,
    };
  } else {
    nodeConfigFrom.value = { triggerType: 9, ...props.lineAttr };
  }

  showDialog.value = true;
});
onUnmounted(() => {
  reset();
});
</script>
<style scoped lang="scss"></style>
